package com.qianye.service.impl;


import com.qianye.pojo.entity.Greet;
import com.qianye.mapper.GreetDao;
import com.qianye.service.GreetBizService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("greetBizService")
public class GreetBizServiceImpl implements GreetBizService {
    @Resource
    private GreetDao greetDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Greet queryById(Long id) {
        return this.greetDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param greet 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Greet> queryByPage(Greet greet, PageRequest pageRequest) {
        long total = this.greetDao.count(greet);
        return new PageImpl<>(this.greetDao.queryAllByLimit(greet, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param greet 实例对象
     * @return 实例对象
     */
    @Override
    public Greet insert(Greet greet) {
        this.greetDao.insert(greet);
        return greet;
    }

    /**
     * 修改数据
     *
     * @param greet 实例对象
     * @return 实例对象
     */
    @Override
    public Greet update(Greet greet) {
        this.greetDao.update(greet);
        return this.queryById(greet.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.greetDao.deleteById(id) > 0;
    }
}
